<template>
  <div class="">
    <div class="head">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
        <el-form-item label="卡名称：" prop="">
          <el-input v-model="queryParams.cardName" placeholder="请输入卡名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="发行方式：" prop="">
          <el-select v-model="queryParams.issueType" placeholder="请选择" clearable style="width: 240px">
            <el-option v-for="dict in issueTypeLs" :key="dict.id" :label="dict.value" :value="dict.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态：" prop="">
          <el-select v-model="queryParams.sellStatus" placeholder="请选择" clearable style="width: 240px">
            <el-option v-for="(item,index) in sellStatusLs" :key="index" :label="item.value" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="可售时间：" prop="">
          <el-date-picker v-model="date" type="daterange" value-format="yyyy-MM-dd" range-separator="至"
            start-placeholder="开始日期" end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发行方：" prop="">
          <el-input v-model="queryParams.name" placeholder="请输入发行方" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="listBox">
      <div class="listBox_head">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">上架新权益卡</el-button>
          </el-col>
        </el-row>
      </div>
      <el-table v-loading="loading" :data="equityCardIssueList" stripe border>
        <el-table-column label="序号" align="center" type="index" width="64px" />
        <el-table-column label="卡名称" align="center" prop="cardName" :show-overflow-tooltip="true" />
        <el-table-column label="发行方" align="center" prop="name" width="200" :show-overflow-tooltip="true" />
        <el-table-column label="发行方式" align="center" prop="issueTypeStr" :show-overflow-tooltip="true">
        </el-table-column>
        <!-- <el-table-column label="状态" align="center" prop="status" :show-overflow-tooltip="true" /> -->
        <el-table-column label="状态" align="center" prop="sellStatusStr" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="总量（张）" align="center" prop="issueTotal" :show-overflow-tooltip="true" />
        <el-table-column label="单价（¥）" align="center" prop="price" :show-overflow-tooltip="true" />
        <!-- <el-table-column label="预约/已售（张）" align="center" prop="num" :show-overflow-tooltip="true" /> -->
        <el-table-column label="预约人数" align="center" prop="subscribeNum" :show-overflow-tooltip="true" />
        <el-table-column label="虚拟预约人数" align="center" prop="virtualSubscribe" :show-overflow-tooltip="true" />
        <el-table-column label="已售数量" align="center" prop="used" :show-overflow-tooltip="true" />
        <el-table-column label="可售时间/申购执行时间" align="center" prop="time" :show-overflow-tooltip="true" width="280px">
          <template slot-scope="scope">
            <span>{{scope.row.startTime}}</span> ~ <span>{{scope.row.endTime}}</span>
          </template>
        </el-table-column>
        <el-table-column label="排序号" align="center" prop="sort" :show-overflow-tooltip="true" />
        <el-table-column label="操作" align="center" width="240" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleView(scope.row)">详情</el-button>
            <el-button size="mini" type="text" @click="cardBill(scope.row)">销售账单</el-button>
            <!-- <el-button size="mini" type="text" @click="jumpCardmember(scope.row)">持卡会员</el-button> -->
            <el-button size="mini" type="text" v-if="scope.row.status==2" @click="updateIssueStatus(scope.row,1)">上架
            </el-button>
            <el-button size="mini" type="text" v-if="scope.row.status==1" @click="updateIssueStatus(scope.row,2)">下架
            </el-button>
            <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
              v-if="scope.row.sellStatus==1||scope.row.sellStatus==5">修改</el-button>
            <el-button size="mini" type="text" @click="sellOut(scope.row)" v-if="scope.row.issueType==3">当前售罄
            </el-button>
            <el-button size="mini" type="text" @click="handleWhiteList(scope.row)" v-if="scope.row.issueType==3">白名单
            </el-button>
            <el-button size="mini" type="text" @click="handleSubscribe(scope.row)"
              v-if="scope.row.issueType==3||scope.row.issueType==2">预约明细</el-button>
            <el-button size="mini" type="text" @click="setUpSort(scope.row)">设置排序号</el-button>
            <el-button size="mini" type="text" @click="drivingSY(scope.row)" v-if="scope.row.isShenYe">打入神椰账号</el-button>
            <el-button size="mini" type="text" @click="virtualReservation(scope.row)" v-if="scope.row.issueType==3">设置虚拟预约人数</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>
    <Dialog ref="dialog" @refresh="getList"></Dialog>
    <DialogwhiteList ref="dialogwhiteList"></DialogwhiteList>
    <DialogSubscribe ref="dialogSubscribe"></DialogSubscribe>
  </div>
</template>

<script>
  import {
    listEquityCardIssue,
    updateIssueStatus,
    exportEquityCardIssue,
    soldOut,
    equityCardIssueSort,
    sendShenyeAccount,
    virtualSubscribe
  } from "@/api/equityManager/equityCardIssue";
  import Dialog from "@/views/equityManager/equityCardIssue/dialog";
  import DialogwhiteList from "@/views/equityManager/equityCardIssue/dialogwhiteList";
  import DialogSubscribe from "@/views/equityManager/equityCardIssue/dialogSubscribe";
  export default {
    // dicts: ['equity_card_issue_type', 'equity_card_sell_status'],
    name: "",
    components: {
      Dialog,
      DialogwhiteList,
      DialogSubscribe
    },
    data() {
      return {
        //日期
        date: [],
        // 遮罩层
        loading: false,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 发行管理表格数据
        equityCardIssueList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          cardName: '', //权益卡名称
          startTime: '', //可售开始时间
          endTime: '', //可售结束时间
          issueType: '', //发行方式
          name: '', //发行方
          sellStatus: '', //销售状态 1 未开售 2出售中 3暂停出售 4 已售完 5未开放预约 6 开放预约 7 中签购买
        },
        issueTypeLs: [], //发行方式下啦
        sellStatusLs: [], //状态下拉
      };
    },
    created() {
      this.getDictIssueType();
      this.getDictSellStatus();
      this.getList();
      console.log(this.dict)
    },
    methods: {
      //获取发行方式字典
      getDictIssueType() {
        var dictType = "equity_card_issue_type";
        this.$getSelectList(dictType)
          .then((res) => {
            this.issueTypeLs = res.result[0].dictList;
          })
          .finally(() => {
            this.loading = false;
          });
      },
      //获取状态字典
      getDictSellStatus() {
        var dictType = "equity_card_sell_status";
        this.$getSelectList(dictType)
          .then((res) => {
            this.sellStatusLs = res.result[0].dictList;
          })
          .finally(() => {
            this.loading = false;
          });
      },
      /** 查询发行管理列表 */
      getList() {
        this.loading = true;
        listEquityCardIssue(this.queryParams).then(response => {
          this.equityCardIssueList = response.result.records;
          this.total = response.result.total;
          this.loading = false;
          this.equityCardIssueList.forEach((item)=>{ //判断是否显示打入神椰账号按钮
            item.isShenYe = false
            if(item.issueType=='1'&&item.sellStatus=='4'&& Number(item.issueInventoryUsable)>0){
              item.isShenYe = true
            }else if(item.issueType=='3' && (item.sellStatus=='14'||item.sellStatus=='16') && (Number(item.buyIssueUsable) + Number(item.whiteIssueUsable) ) > 0){
              item.isShenYe = true
            }else{
              item.isShenYe = false
            }
          })
          console.log(this.equityCardIssueList)
        });
      },
      // 表单重置
      reset() {
        this.date = []
        this.queryParams = {
          pageNum: 1,
          pageSize: 10,
          cardName: '', //权益卡名称
          startTime: '', //可售开始时间
          endTime: '', //可售结束时间
          issueType: '', //发行方式
          name: '', //发行方
          sellStatus: '', //销售状态 1 未开售 2出售中 3暂停出售 4 已售完 5未开放预约 6 开放预约 7 中签购买
        }
        // this.resetForm("form");
        this.handleQuery()
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.startTime = this.date[0];
        this.queryParams.endTime = this.date[1];
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        // this.resetForm("queryForm");
        this.reset();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.$refs.dialog.open();
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.$refs.dialog.open(row.id, false, row.cardId);
      },
      /** 详情按钮操作 */
      handleView(row) {
        this.$refs.dialog.open(row.id, true, row.cardId);
      },
      /** 白名单按钮操作 */
      handleWhiteList(row) {
        this.$refs.dialogwhiteList.open(row.id);
      },
      /** 预约明细按钮操作 */
      handleSubscribe(row) {
        this.$refs.dialogSubscribe.open(row.id);
      },
      /** 设置排序号 */
      setUpSort(row) {
        this.$prompt('请输入排序号', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputValue: row.sort,
          closeOnClickModal: false,
          inputPattern: /^\d{1,4}$/,
          inputErrorMessage: '只能输入最多4位正整数'
        }).then(({
          value
        }) => {
          // console.log(value)
          let data = {
            id: row.id,
            sort: value,
          }
          equityCardIssueSort(data).then(response => {
            this.$message({
              type: 'success',
              message: '设置成功'
            });
            this.resetQuery()
            // this.getList();
          });
        }).catch(() => {
          // this.$message({
          //   type: 'info',
          //   message: '取消输入'
          // });       
        });
      },
      /** 设置虚拟预约人数 */
      virtualReservation(row) {
        this.$prompt('修改虚拟预约人数', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputValue: Number(row.virtualSubscribe),
          closeOnClickModal: false,
          inputPattern: /^[0-9]*$/,
          inputErrorMessage: '请输入大于等于0的正整数'
        }).then(({
          value
        }) => {
          // console.log(value)
          let data = {
            id: row.id,
            virtualSubscribe: value,
          }
          virtualSubscribe(data).then(response => {
            this.$message({
              type: 'success',
              message: '操作成功'
            });
            this.getList();
          });
        }).catch(() => {
          // this.$message({
          //   type: 'info',
          //   message: '取消输入'
          // });       
        });
      },
      /** 上下架按钮操作 */
      updateIssueStatus(row, status) {
        let title = '上架'
        if (status == 1) { //上架
          title = '上架'
        } else {
          title = '下架'
        }
        this.$confirm('是否' + title, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let data = {
            id: row.id,
            status: status,
          }
          updateIssueStatus(data).then(response => {
            this.$message({
              type: 'success',
              message: title + '成功!'
            });
            // this.resetQuery()
            this.getList();
          });

        }).catch(() => {});
      },
      /** 当前售罄按钮操作 */
      sellOut(row) {
        this.$confirm('是否执行此操作', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let data = {
            id: row.id,
          }
          soldOut(data).then(response => {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            // this.resetQuery()
            this.getList();
          });

        }).catch(() => {});
      },
      /** 持卡会员 */
      jumpCardmember(row) {
        this.$router.push({
          path: "cardmember",
          query: {
            id: row.cardId
          },
        });
      },
      /** 账单 */
      cardBill(row) {
        this.$router.push({
          path: "/moneyManagement/equityCardBill-list",
          query: {
            name: row.cardName,
          },
        })
      },
      /** 打入神椰账号按钮操作 */
      drivingSY(row) {
        this.$confirm('是否打入神椰账号', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          sendShenyeAccount(row.id).then(response => {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getList();
          });
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$modal.confirm('是否确认导出所有发行管理数据项？').then(() => {
          this.exportLoading = true;
          return exportEquityCardIssue(queryParams);
        }).then(response => {
          this.$download.name(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
      }
    }
  };

</script>
<style scoped>
  .head {
    box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    padding: 20px 20px 0;
  }

  .listBox {
    box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    padding: 20px 20px;
    margin-top: 10px;
  }

  .listBox_head {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-bottom: 20px;
  }

</style>
